/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.office;

import java.awt.*;
import java.awt.geom.*;

import twaver.*;
import twaver.network.*;
import twaver.network.ui.*;

public class BusLinkUI extends NodeUI {
    public BusLinkUI(TNetwork network, BusLink node) {
        super(network, node);
    }

    public void paintBody(Graphics2D g2d) {
        Color color = getColor();
        Object oldHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setColor(Color.white);
        Area right = getRightShape();
        g2d.fill(right);
        g2d.setColor(color.darker().darker());
        g2d.setStroke(TWaverUtil.createStroke(4));
        g2d.draw(getRightSmallShape());

        g2d.setPaint(getPaint(color));
        Area body = getShape();
        g2d.fill(body);

        g2d.setStroke(TWaverUtil.createStroke(1));
        g2d.setColor(Color.darkGray);
        body.add(right);
        g2d.draw(body);

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldHint);
    }

    private Area getShape() {
        Node node = getNode();

        int x = node.getLocation().x;
        int y = node.getLocation().y;
        int width = node.getHeight() / 2;
        int height = node.getHeight()-1;
        Ellipse2D.Float left = new Ellipse2D.Float(x, y, width, height);

        x = node.getLocation().x + node.getWidth() - node.getHeight() / 2;
        y = node.getLocation().y;
        width = node.getHeight() / 2;
        height = node.getHeight()-1;
        Ellipse2D.Float right = new Ellipse2D.Float(x, y, width, height);

        x = node.getLocation().x + node.getHeight() / 4;
        y = node.getLocation().y;
        width = node.getWidth() - node.getHeight() / 2;
        height = node.getHeight()-1;
        Rectangle middle = new Rectangle(x, y, width, height);

        Area area = new Area(left);
        area.add(new Area(middle));
        area.subtract(new Area(right));

        return area;
    }

    private Area getRightShape() {
        Node node = getNode();

        int x = node.getLocation().x + node.getWidth() - node.getHeight() / 2;
        int y = node.getLocation().y;
        int width = node.getHeight() / 2;
        int height = node.getHeight()-1;
        Ellipse2D.Float right = new Ellipse2D.Float(x, y, width, height);
        return new Area(right);
    }

    private Area getRightSmallShape() {
        Node node = getNode();

        int x = node.getLocation().x + node.getWidth() - node.getHeight() / 2 + 1;
        int y = node.getLocation().y + 2;
        int width = node.getHeight() / 2 - 3;
        int height = node.getHeight() - 4;
        Ellipse2D.Float right = new Ellipse2D.Float(x, y, width, height);
        return new Area(right);
    }


    private Paint getPaint(Color color) {
        Node node = getNode();
        int x1 = node.getLocation().x;
        int y1 = node.getLocation().y;
        int x2 = node.getLocation().x;
        int y2 = node.getLocation().y + node.getHeight() / 2;

        GradientPaint paint = new GradientPaint(x1,
                                                y1,
                                                color.darker().darker(),
                                                x2,
                                                y2,
                                                color,
                                                true);
        return paint;
    }
}